package net.hymnal.church;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;

import android.util.Log;

public class Settings {
	
	public static final int    MAX_TEXT_HYMN_SIZE = 80;
	public static final int    MIN_TEXT_HYMN_SIZE = 12;
	
	public static final String splitSeparator = "|";
	
	
	public static final String youngHymnalPath = "young/";
	public static final String commonHymnalPath = "common_gen/";
	public static final String commonHymnalMelodyPath = "common_melody_gen/";
	public static final String tableCommonPath = "tables/";
	public static final String tableMelodyFileName = "common_melody_gen2.txt";
	static final int MAX_COMMON_HYMNS = 551;
	static final int MAX_YOUNG_HYMNS  = 186;
	
	final String settingFileName = "setting.dat";	
	
	private final int CURRENT_SETTING_FILE_SIZE = 5;

	final byte DEF_TEXT_SIZE = 24;
	
	final int DEF_THEME_ID = android.R.style.Theme_Light;

	public byte mTextSize;
	public int themeID;
	boolean themeColorWhite = false;

	public Settings() {
		mTextSize = (byte) DEF_TEXT_SIZE;
		themeID = DEF_THEME_ID;
	}
	

	void loadSetting(File dir) throws IOException {	
		
		File file = null;
		FileReader fileReader = null;		
		try{
    	    file = new File(dir, settingFileName);	
   	 	    fileReader = new FileReader(file);
		} catch (NullPointerException npe) {			
            System.out.println("NullPointerException: Wrong parametr:" + npe.getMessage() + " " + settingFileName);
		} catch (FileNotFoundException fne) {
			System.out.println("FileNotFoundException: " + fne.getMessage() + " " + settingFileName);			
		} finally {
			if ((file != null) && (fileReader != null)) {
				char[] fileChar = new char[CURRENT_SETTING_FILE_SIZE];
				int readBytes = -1;
				try {
					readBytes = fileReader.read(fileChar);
				} catch (IOException ioe) {
					Log.e("File read error:", ioe.getMessage());
					return;
				}
				if (readBytes < 0) {
					throw new IOException(
							"The data can not be read from file: " + settingFileName);
				}				
				
				if (readBytes == CURRENT_SETTING_FILE_SIZE) {
					
					mTextSize = (byte) fileChar[0];
				
					themeID = getInt(fileReader, fileChar, 1);
				
				}
				fileReader.close();				
			}			
		}   	 	
	}

	void saveSetting(File dir) throws IOException {
		int readResult = 0;
		File file = null;
		FileWriter fileWrriter = null; 
		try{
    	    file = new File(dir, settingFileName);	
   	 	    fileWrriter  = new FileWriter(file);
		} catch (NullPointerException npe) {			
			System.out.println("NullPointerException: Wrong parametr:" + npe.getMessage() + " " + settingFileName);
		} catch (FileNotFoundException fne) {
			System.out.println("FileNotFoundException: " + fne.getMessage() + " " + settingFileName);
		} finally {
			if (fileWrriter != null) {
				fileWrriter.write((char) mTextSize);
				mTextSize = (byte) readResult;				
				saveInt(fileWrriter, themeID);								
				fileWrriter.close();
			}
		}
	}
	void switchTheme(){
		if(themeID == android.R.style.Theme_Light) {
			themeID = android.R.style.Theme_Black;
		} else {
			themeID = android.R.style.Theme_Light;
		}		
	}

	public int getCurrentTheme() {
		return themeID;
	}
	
	public boolean isBlack() {
		if (themeID == android.R.style.Theme_Light) {
			return false;
		}
		return true;
	}
	
	static void saveInt(FileWriter fw, int num) throws IOException{
		byte[] aByte = ByteBuffer.allocate(4).putInt(num).array();
		char[] aChar = new char[4];
		for(int i = 0; i < aChar.length; i++){
			aChar[i] = (char)aByte[i]; 
		}
		fw.write(aChar);		
	}
	
	/*
	 * Bytes size should be (4 + offset).
	 */
	static int getInt(FileReader fr,char[] bytes, int offset) throws IOException{
		int num = (int)(bytes[3 + offset] & 0x000000ff) +
				  (int)(((bytes[2 + offset] & 0x000000ff)) << 8) + 
				  (int)(((bytes[1 + offset] & 0x000000ff)) << 16) +
				  (int)((bytes[0 + offset] & 0x000000ff) << 24);
		return num;
	}	
}
